{% from "macros/variables.html" import tlp_levels %}
{% from "macros/forms.html" import form_input_content, form_textarea, form_switch %}

{% macro attribute_field(attr, read_only=False, stories=[]) %}
  {% set base_name = 'attributes[' ~ attr.id ~ ']' %}
  {% set field_id = 'attribute-' ~ attr.id %}
  {% set value = attr.value | default('') %}
  {% set label = attr.title or ('Attribute #' ~ attr.id) %}
  {% set description = attr.description %}
  {% set required = attr.required %}
  <div class="mb-5">
    {% if attr.type in ['TEXT', 'RICH_TEXT'] %}
      {{ form_textarea(name=base_name, value=value, placeholder=description, required=required) }}
    {% elif attr.type == 'STRING' %}
      {{ form_input_content(name=base_name, value=value, placeholder=description, required=required, input_id=field_id) }}
    {% elif attr.type == 'NUMBER' %}
      {{ form_input_content(name=base_name, value=value, type='number', placeholder=description, required=required, input_id=field_id) }}
    {% elif attr.type == 'BOOLEAN' %}
      {{ form_switch(name=base_name, value=value, label=label, required=required, input_id=field_id) }}
    {% elif attr.type in ['ENUM', 'CPE'] %}
      <label class="select w-full">
        <span class="label justify-between">
          <span>{{ label }}</span>
          {% if required %}<span class="text-error text-sm">*</span>{% endif %}
        </span>
        <select name="{{ base_name }}" class="select select-bordered w-full" {% if read_only %}disabled{% endif %}>
          {% if not value %}<option value="" disabled selected>Select an option</option>{% endif %}
          {% for option in attr.render_data.attribute_enums | default([]) %}
            <option value="{{ option.value }}" {% if option.value == value %}selected{% endif %}>{{ option.value }}</option>
          {% endfor %}
        </select>
      </label>
      {% if read_only %}<input type="hidden" name="{{ base_name }}" value="{{ value }}">{% endif %}
      {% if description %}<p class="text-xs text-base-content/60 mt-1">{{ description }}</p>{% endif %}

    {% elif attr.type == 'RADIO' %}
      <fieldset class="fieldset border border-base-300 rounded-box px-4 py-3">
        <legend class="fieldset-legend text-sm font-semibold">
          {{ label }}
          {% if required %}<span class="text-error ml-1">*</span>{% endif %}
        </legend>
        <div class="flex flex-wrap gap-3">
          {% for option in attr.render_data.attribute_enums | default([]) %}
            <label class="flex items-center gap-2">
              <input type="radio"
                     name="{{ base_name }}"
                     value="{{ option.value }}"
                     class="radio"
                     {% if option.value == value %}checked{% endif %}
                     {% if read_only %}disabled{% endif %}>
              <span>{{ option.value }}</span>
            </label>
          {% endfor %}
        </div>
      </fieldset>

    {% elif attr.type == 'TLP' %}
      <div class="floating-label w-full">{{ tlp_levels(value, name=base_name) }}</div>

    {% elif attr.type in ['DATE', 'DATE_TIME', 'TIME'] %}
      <label class="input w-full">
        <span class="label">{{ label }}
          {% if required %}<span class="text-error text-sm">*</span>{% endif %}
        </span>
        <input type="date"
               name="{{ base_name }}"
               value="{{ value }}"
               placeholder="{{ description or 'Enter a date' }}"
               {% if read_only %}readonly{% endif %}>
      </label>

    {% elif attr.type == 'CVE' %}
      {{ form_input_content(name=base_name, value=value, pattern="^$|CVE-\\d{4}-\\d{4,7}$", invalid_msg="Please enter a valid CVE in the form CVE-YYYY-NNNN", placeholder=description or 'Enter CVE in the form CVE-YYYY-NNNN', read_only=read_only, input_id=field_id) }}

    {% elif attr.type == 'CVSS' %}
      {{ form_input_content(name=base_name, value=value, pattern="^$|^(0(\\.\\d{1,2})?|[1-9](\\.\\d{1,2})?|10(\\.0{1,2})?)$", invalid_msg="Please enter a valid CVSS score between 0.0 and 10.0", placeholder=description or 'Enter CVSS score (0.0 - 10.0)', read_only=read_only, input_id=field_id) }}
      <p class="text-xs text-base-content/60 mt-1">
        Use the <a href="https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator" target="_blank">NVD CVSS calculator</a> to determine the score.
      </p>

    {% elif attr.type == 'STORY' %}
      {% set selected_values = value.split(',') if value else [] %}
      <div class="w-full">
        <select id="attribute_story_{{ attr.id }}" name="{{ base_name }}[]" multiple>
          {% for story in stories %}
            <option value="{{ story.id }}" {% if story.id in selected_values %}selected{% endif %}>{{ story.title }}</option>
          {% endfor %}
        </select>
      </div>

    {% elif attr.type == 'ATTACHMENT' %}
      <div class="form-control w-full">
        <span class="label justify-between">
          <span>{{ label }}</span>
          {% if required %}<span class="text-error text-sm">*</span>{% endif %}
        </span>
        <input type="hidden" name="{{ base_name }}" value="{{ value }}">
        <div class="alert alert-info">Attachment editing is not yet available in this view. Existing data will be preserved on save.</div>
        {% if value %}<p class="text-xs text-base-content/60 mt-1">Stored attachment reference: {{ value }}</p>{% endif %}
      </div>

    {% else %}
      <input type="hidden" name="{{ base_name }}" value="{{ value }}">
      <div class="alert alert-warning">Unsupported attribute type "{{ attr.type }}" is kept as read-only.</div>
    {% endif %}
  </div>
{% endmacro %}
